package org.paradise.dms.services.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.paradise.dms.dao.impl.StudentBedLinkDAOImpl;
import org.paradise.dms.dao.impl.StudentDAOImpl;
import org.paradise.dms.dao.impl.StudentReportDAOImpl;
import org.paradise.dms.pojo.AllocationInfo;
import org.paradise.dms.pojo.Student;
import org.paradise.dms.pojo.StudentReportDormCheckout;
import org.paradise.dms.services.StudentReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.DaoException;

@Service
public class StudentReportServiceImpl implements StudentReportService {
	
	private static Logger log = Logger.getLogger(StudentReportServiceImpl.class);
	
	@Autowired
	private StudentReportDAOImpl studentReportDAOImpl;
	
	@Autowired
	private StudentBedLinkDAOImpl studentBedLinkDAOImpl;

	@Autowired
	private StudentDAOImpl studentDAOImpl;

	public StudentBedLinkDAOImpl getStudentBedLinkDAOImpl() {
		return studentBedLinkDAOImpl;
	}

	public void setStudentBedLinkDAOImpl(StudentBedLinkDAOImpl studentBedLinkDAOImpl) {
		this.studentBedLinkDAOImpl = studentBedLinkDAOImpl;
	}

	public String cancelAllocate(String studentNO) {
		String statusMsg = "";
		int result = this.getStudentReportDAOImpl().cancelAllocate(studentNO);
		if(result == this.getStudentReportDAOImpl().NOT_ALLOW_CHECK_OUT_TAG){
			return "已经退宿过一次，不允许再次退宿";
		}
		if(result >=1 ){
			log.info("DMS_info:退宿成功,学号："+studentNO);
		}else{
			statusMsg += "没有住宿信息";
		}
		return statusMsg;
	}

	public List<Object> findOrder(String condition) {
		// TODO Auto-generated method stub
		return null;
	}

	public Student login(String username, String password) {
		Student student = studentReportDAOImpl.getStudent(username, password);
		if(null == student || Student.NOACTIVE == student.getStudentstatusid()) {
			return null;
		}
		return student;
	}
	
	public Student activeStudent(String identitycardno,String password) {
		// TODO Auto-generated method stub
		Student student = studentReportDAOImpl.getStudentByIdentity(identitycardno);
		if(null != student && Student.NOACTIVE == student.getStudentstatusid()) {
			student.setStudentpassword(password);
			student.setStudentstatusid(Student.ACTIVE);
			try {
				studentDAOImpl.update(student);
				return student;
			} catch (DaoException e) {
				log.error("DMS_error:激活学生帐号失败" );
				log.error("DMS_error: 失败原因" + e);
			}
		}
		return null;
	}
	//多人申请
	public String insertMultipleApply(String[] studentNOs) {
		int[] dormitoryids = new int[2];
		if(studentNOs.length <= 4){
			//4人及以下只要一间房间就可以了
			dormitoryids[0] = studentReportDAOImpl.getAvailOneDormitoryidByStudentnosForMultiApply(studentNOs);
		}else if(studentNOs.length > 4){
			//5人及以上需要两个房间才能装下
			dormitoryids = studentReportDAOImpl.getAvailDormitoryidsByStudentnosForMultiApply(studentNOs);
		}
		List<Student> s = studentReportDAOImpl.getStudentsByNOs(studentNOs);
		System.out.println("+++++++"+s.size());
		if(null != s && s.size()==studentNOs.length){
			AllocationInfo aif =  studentReportDAOImpl.getAllocationinfoById(s.get(0).getStudentpreasignedid()+"");
			if(null != aif){
				//从allocationinfo 表中查出的对应preassignid的起止时间
				Date start  = aif.getStarttime();
				Date end = aif.getEndtime();
				String msg = "";
				for(Student stu :  s){
					
					//插入到新生报到退宿记录表（未插入宿舍id,目前并不重要）
					StudentReportDormCheckout srdc= new StudentReportDormCheckout();
					srdc.setStudentid(stu.getStudentid());
					srdc.setStudentno(stu.getStudentno());
					studentReportDAOImpl.insertStudentreportdormcheckout(srdc);
					
					//插入住宿信息
					String tmpMsg = studentBedLinkDAOImpl.insertStudentBedLink(stu.getStudentid(),dormitoryids[0],start,end);
					if("宿舍床位已满 !".equals(tmpMsg)){
						msg += studentBedLinkDAOImpl.insertStudentBedLink(stu.getStudentid(),dormitoryids[1],start,end);
					}else msg += tmpMsg;
				}
				return msg ;
			}else return "无法根据学生的预分配值查到对应预分配信息";
		}else return "无法根据学号集合查到所有学生";
	}

	//单人申请
	public Object insertSingleApply(String studentNO) {
		int dormitoryid = studentReportDAOImpl.getAvailDormitoryidByStudentNo(studentNO);
		Student s = studentReportDAOImpl.getStudentByNO(studentNO);
		if(null != s ){
			AllocationInfo aif =  studentReportDAOImpl.getAllocationinfoById(s.getStudentpreasignedid()+"");
			if(null != aif){
				
				//插入到新生报到退宿记录表（未插入宿舍id,目前并不重要）
				StudentReportDormCheckout srdc= new StudentReportDormCheckout();
				srdc.setStudentid(s.getStudentid());
				srdc.setStudentno(s.getStudentno());
				studentReportDAOImpl.insertStudentreportdormcheckout(srdc);
				
				//从allocationinfo 表中查出的对应preassignid的起止时间
				Date start  = aif.getStarttime();
				Date end = aif.getEndtime();
				//插入住宿信息
				String msg = studentBedLinkDAOImpl.insertStudentBedLink(s.getStudentid(),dormitoryid,start,end);
				return msg ;
			}else return "无法根据学生的预分配值查到对应预分配信息";
		}else return "无法根据学号查到学生";
	}
	public List findDormByStudentno(String stuno){
		return studentReportDAOImpl.findDormByStudentno(stuno);
	}
	
	
	
	
	
	public String validateStudent(String studentno ,String password ){
		Student student = studentReportDAOImpl.getStudent(studentno, password);
		if(null != student) {
			return "账号或密码错误";
		}
		if(null != student && Student.NOACTIVE == student.getStudentstatusid()) {
			return "该学生未激活";
		}else return "yes";
	}
	public Student validateStudentExits(String studentno ,String password){
		return studentReportDAOImpl.getStudent(studentno, password);
	}

	public Student getStudentByNO(String studentno){
		return studentReportDAOImpl.getStudentByNO(studentno);
	}

	public List getAvailDormByPreAsignedid(String sql){
		return studentReportDAOImpl.queryBySQL(sql);
	}
	
	public int getAvailDormCountByPreAsignedid(String sql){
		return studentReportDAOImpl.queryCountBySQL(sql);
	}
	//从新生报道退宿记录表查找退宿记录
	public List findDormCheckOut(String studentno){
		return studentReportDAOImpl.findDormCheckOut(studentno);
		
	}
	
	
	
	
	
	public StudentDAOImpl getStudentDAOImpl() {
		return studentDAOImpl;
	}

	public void setStudentDAOImpl(StudentDAOImpl studentDAOImpl) {
		this.studentDAOImpl = studentDAOImpl;
	}
	
	public StudentReportDAOImpl getStudentReportDAOImpl() {
		return studentReportDAOImpl;
	}

	public void setStudentReportDAOImpl(StudentReportDAOImpl studentReportDAOImpl) {
		this.studentReportDAOImpl = studentReportDAOImpl;
	}
	


}
